import { invoke } from "@tauri-apps/api/tauri";
import { ElLoading,ElNotification } from 'element-plus';

const loadingInstance = {
    target: null,
    count: 0,
}

/**
 * 关闭Loading层实例
 */
function closeLoading(options) {
    if (options.loading) {
        if (loadingInstance.count > 0) loadingInstance.count--
        if (loadingInstance.count === 0) {
            loadingInstance.target.close()
            loadingInstance.target = null
        }
    }
}

export async function doInvoke (method,data,options,loading){
    let datastring = JSON.stringify(data);
    options = Object.assign(
        {
            loading: false, // 是否开启loading层效果, 默认为false
            showErrorMessage: true, // 是否开启接口错误信息展示,默认为true
            showCodeMessage: true, // 是否开启code不为1时的信息提示, 默认为true
            showSuccessMessage: true, // 是否开启code为1时的信息提示, 默认为false
        },
        options
    )
    if (options.loading) {
        loadingInstance.count++
        if (loadingInstance.count === 1) {
            loadingInstance.target = ElLoading.service(loading)
        }
    }
    return await invoke("docom",{ method:method,data:datastring }).then(
        (message) => {
            console.log(message);
            let res = JSON.parse(message);
            if (options.showCodeMessage && res.code != 0) {
                ElNotification({
                    type: 'error',
                    message: res.msg,
                })
            }
            if(options.showSuccessMessage && res.code == 0){
                ElNotification({
                    type: 'success',
                    message: res.msg,
                })
            }
            closeLoading(options)
            return res.data;
        }
    ).catch(
        (error) => {
            closeLoading(options)
            if(options.showErrorMessage){
                ElNotification({
                    type: 'error',
                    message: error,
                })
            }
        }
    )
}

export async function doConnInvoke (method,data,options,loading){
    let datastring = JSON.stringify(data);
    options = Object.assign(
        {
            loading: false, // 是否开启loading层效果, 默认为false
            showErrorMessage: true, // 是否开启接口错误信息展示,默认为true
            showCodeMessage: true, // 是否开启code不为1时的信息提示, 默认为true
            showSuccessMessage: true, // 是否开启code为1时的信息提示, 默认为false
        },
        options
    )
    if (options.loading) {
        loadingInstance.count++
        if (loadingInstance.count === 1) {
            loadingInstance.target = ElLoading.service(loading)
        }
    }
    return await invoke("docom",{ method:method,data:datastring }).then(
        (message) => {
            let res = JSON.parse(message);
            if (options.showCodeMessage && res.code != 0) {
                ElNotification({
                    type: 'error',
                    message: res.message,
                })
            }
            if(options.showSuccessMessage && res.code == 0){
                ElNotification({
                    type: 'success',
                    message: res.message,
                })
            }
            closeLoading(options)
            console.log(message)
            return res.data;
        }
    ).catch(
        (error) => {
            console.log(error)
            closeLoading(options)
            if(options.showErrorMessage){
                ElNotification({
                    type: 'success',
                    message: error,
                })
            }
        }
    )
}
